import request from '../utils/request'

// 优惠券相关接口
const couponApi = {
   // 获取限时抢购优惠券
   getFlashSaleCoupons: () => {
      return request.get('/coupon-service/couponKill/selectCouponKill') 
   },
   // 获取普通优惠券列表
   getNormalCoupons: () => {
      return request.get('/coupon-service/coupon/selectCoupon')
   },
   // 获取我的优惠券
   getMyCoupons: () => {
      // 获取用户ID
      const userId = uni.getStorageSync('user')?.userId; 
      console.log('用户 ID:', userId);
      if (!userId) {
        console.error('未获取到用户 ID');
        return Promise.reject(new Error('未获取到用户 ID'));
      }
      return request.get('/coupon-service/userCoupon/selectMyCoupon?userId='+userId)
   },
   // 领取优惠券
   getCoupon: (data) => {
      // 获取用户ID
      const userId = uni.getStorageSync('user')?.userId;
      console.log('用户 ID:', userId);
      if (!userId) {
        console.error('未获取到用户 ID');
        return Promise.reject(new Error('未获取到用户 ID'));
      }
      // 合并用户ID到请求数据中
      data.userId = userId;
      console.log('请求数据:', data);
      const userCoupons = {
        userId: userId,
        templateId: data.templateId
      }
      console.log('userCoupons:', userCoupons);
      return request.post('/coupon-service/userCoupon/getCoupon', userCoupons) 
   },
   // 抢购优惠券
   seckillCoupon: (data) => {
     // 获取用户ID
      const userId = uni.getStorageSync('user')?.userId; 
      console.log('用户 ID:', userId);
      if (!userId) {
        console.error('未获取到用户 ID');
        return Promise.reject(new Error('未获取到用户 ID'));
      }
      return request.post(`/coupon-service/couponKill/couponKill?userId=${userId}`, data)
   }
}

export default couponApi